import { createRouter, createWebHistory } from "vue-router";
import { useUserStore } from '@/stores/user';

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      redirect: "/index",
    },
    {
      path: "/index",
      name: "index",
      component: () => import("@/views/book_list_show/index.vue"),
    },
    
    {
      path: "/my",
      name: "myinfo",
      component: () => import("@/views/my/index.vue"),
    },
    {
      path: "/login",
      name: "login",
      component: () => import("@/views/user_center/login/index.vue"),
    },
    { 
      path: '/user/auth/github/callback', 
      name: 'github-callback', 
      component:() => import("@/views/GitHubCallback.vue")
    },
    {
      path: "/register",
      name: "register",
      component: () => import("@/views/user_center/register/index.vue"),
    },
  

    {
      path: "/article_list_search",
      name: "article_list_search",
      component: () => import("@/views/article_list_show/index.vue"),
    },
    {
      path:"/chat",
      name:"chat",
      component:()=>import("@/views/chat.vue")
    },
    {
      path:"/write",
      name:"write",
      component:()=>import("@/views/write.vue")
    },
    {
      path:"/writer_center",
      name:"writer_center",
      component:()=>import("@/views/writer_center/index.vue")
    },
    {
      path:"/user_center",
      name:"user_center",
      component:()=>import("@/views/user_center/index.vue")
    },
    {
      path:"/user_info/:id",
      name:"user_info",
      component:()=>import("@/views/user_center/user_info/index.vue")
    },
    {
      path:"/book_show/:id",
      name:"book_show",
      component:()=>import("@/views/book_list_show/book_show/index.vue")
    },
    {
      path:"/book_create",
      name:"book_create",
      component:()=>import("@/views/writer_center/book_create/index.vue")
    },

    {
      path:"/chapter_read/:id",
      name:"chapter_read",
      component:()=>import("@/views/book_list_show/chapter_show/index.vue")
    },
    {
      path:"/chapter_write/:id",
      name:"chapter_write",
      component:()=>import("@/views/writer_center/chapter_write/index.vue")
    },
    {
      path:"/chapter_update/:id",
      name:"chapter_update",
      component:()=>import("@/views/writer_center/chapter_update/index.vue")
    },
    {
      path:"/test",
      name:"test",
      component:()=>import("@/views/writer_center/components/totalAnalysis.vue")
    },
    {
      path:"/product_list_show",
      name:"product_list_show",
      component:()=>import("@/views/product_list_show/index.vue")
    },
    {
      path:"/product_detail/:id",
      name:"product_detail",
      component:()=>import("@/views/product_list_show/product_detail/index.vue")
    },
    {
      path:"/product_create",
      name:"product_create",
      component:()=>import("@/views/product_create/index.vue")
    },
    {
      path:"/order_detail/:id",
      name:"order_detail",
      component:()=>import("@/views/order_detail/index.vue")
    },
    {
      path:"/rank",
      name:"rank",
      component:()=>import("@/views/rank/index.vue")
    }
    ,
    {
      path:"/system_notice",
      name:"system_notice",
      component:()=>import("@/views/system_notice/index.vue")
    },
    {
      path:"/order_list_show",
      name:"order_list_show",
      component:()=>import("@/views/order_list_show/index.vue")
    },
    {
      path:"/order_pay/:token",
      name:"order_pay",
      component:()=>import("@/views/order_list_show/order_pay/index.vue")
    },
    {
      path:"/article_write",
      name:"article_write",
      component:()=>import("@/views/writer_center/article_write/index.vue")
    } ,   {
      path: "/article/:id",
      name: "article",
      component: () => import("@/views/article_list_show/article_show/index.vue"),
    },

   
    {
      path:"/buffet_list_show",
      name:"buffet_list_show",
      component:()=>import("@/views/buffet_list_show/index.vue")
    },
    {
      path:"/buffet/:id",
      name:"buffet",
      component:()=>import("@/views/buffet_list_show/buffet/index.vue")
    },
    {
      path:"/manage",
      name:"manage",
      component:()=>import("@/views/manage/index.vue")
    }
  ],
});

// 导航守卫
router.beforeEach((to, from, next) => {
  const user = useUserStore();
  var my_id: number = user.loginstatus ? user.userInfo.id : -1;

  // 如果用户没有登录且目标路由是'write'
  if (my_id === -1 && to.name == 'write') {
    next({ name: 'login' });
  } else {
    next();
  }
});

export default router;
